package qf.index;//package com.qf.index;
//
//import cn.hutool.core.util.NumberUtil;
//import cn.hutool.db.Db;
//import cn.hutool.db.Entity;
//import cn.hutool.json.JSONUtil;
//import com.alibaba.fastjson.JSONObject;
//import lombok.Data;
//
//import java.io.Serializable;
//import java.sql.SQLException;
//import java.text.SimpleDateFormat;
//import java.util.*;
//import java.util.concurrent.ConcurrentHashMap;
//
//public class TestA {
//
//    public static void main(String[] args) throws SQLException {
//
//        String newDataName = "scholar_25_02.";
//        String summaryTable = newDataName + "unified_search_summary_0120_old";
//        String enBasicDeptTable = newDataName + "en_basic_hosp_dept";
//        String cnBasicDeptTable = newDataName + "cn_basic_hosp_dept";
//        String basicUnivCollTable = newDataName + "basic_univ_coll";
//        String htLocationChinaTable = newDataName + "ht_location_china";
//        String basicHospUnivDeptTable = newDataName + "basic_hosp_or_univ_dept";
//        String hospUnivInfoSumTable = newDataName + "hosp_or_univ_info_sum";
//        String hospUnivDeptTable = "scholar_25_02.test_hosp_or_univ_dept_unit";
/// /        Db.use().execute("truncate table " + hospUnivDeptTable);
/// /        rangeTable(summaryTable, "id", "c1_au`,`year", htLocationChinaTable, basicUnivCollTable, enBasicDeptTable, cnBasicDeptTable, basicHospUnivDeptTable, hospUnivInfoSumTable);
//
//        Map<String, InfoFuDanStemEdu> infoFuDanStemYouDaoMap = new HashMap<>();
//        getInfoFuDanStemYouDao(cnBasicDeptTable, "id", "you_dao`,`stem`,`fudan`,`info", infoFuDanStemYouDaoMap, "stem");
//        System.out.println(cnBasicDeptTable + "--" + infoFuDanStemYouDaoMap.size());
//        getInfoFuDanStemYouDao(enBasicDeptTable, "id", "you_dao`,`stem`,`fudan`,`info", infoFuDanStemYouDaoMap, "stem");
//        System.out.println(enBasicDeptTable + "--" + infoFuDanStemYouDaoMap.size());
//        updateBasicHospUnivDeptTable(basicHospUnivDeptTable, "id", "info", infoFuDanStemYouDaoMap, "stem");
//        getInfoFuDanStemYouDao(basicUnivCollTable, "id", "edu`,`info", infoFuDanStemYouDaoMap, "edu");
//        System.out.println(basicUnivCollTable + "--" + infoFuDanStemYouDaoMap.size());
//        updateBasicHospUnivDeptTable(basicHospUnivDeptTable, "id", "info", infoFuDanStemYouDaoMap, "edu");
//
//    }
//
//    public static void rangeTable(String summaryTable, String autoId, String field, String htLocationChinaTable, String basicUnivCollTable, String enBasicDeptTable, String cnBasicDeptTable, String basicHospUnivDeptTable, String hospUnivInfoSumTable) throws SQLException {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        List<Entity> tableNum = Db.use().query("select count(1) as count from " + summaryTable);
//        String limit = "";
////        String limit = " limit 1";
//        int onePageNum = 10000;
//        Map<String, String> standardMap = new ConcurrentHashMap<>();
//        standardMap.put("department medical", "Department Medical");
////        Map<Set<String>, Set<String>> unitAndSumMap = new ConcurrentHashMap<>();
//        Map<String, Set<String>> deptAndInfoSetMap = new ConcurrentHashMap<>();
//        Map<String, Integer> deptAndSumMap = new ConcurrentHashMap<>();
//        Map<String, Set<String>> unitAndInfoSetMap = new ConcurrentHashMap<>();
//        Map<String, Integer> unitAndSumMap = new ConcurrentHashMap<>();
//        Map<String, String> countryMap = cList.getCountryMap();
//        Set<String> acadSet = uSet.getAcadSet();
//        Set<String> centerSet = uSet.getCenterSet();
//        Set<String> collSet = uSet.getCollSet();
//        Set<String> hospSet = uSet.getHospSet();
//        Set<String> univSet = uSet.getUnivSet();
//        Set<String> removeSet = uSet.getRemoveSet();
//        Set<String> dellSet = uSet.getDellSet();
//        Set<String> stemSet = uSet.getStemSet();
//        Set<String> fuDanSet = uSet.getFuDanSet();
//        Set<String> chinaSet = cList.chinaSet();
//        Set<String> notCollSet = cList.getNotCollSet();
//        Set<String> citySet = new HashSet<>();
//        Set<String> subjectSet = cList.getSubjectSet();
//        Set<String> provCitySet = Dept.getProvCitySet(htLocationChinaTable, "id", "prov`,`name", citySet);
//        Map<String, En.Edu> collAndEduMap = new HashMap<>();
//        Set<String> toDeptSet = uSet.toDept();
//        Map<String, List<String>> deptAndFuDanOrStemListMap = new HashMap<>();
////        Map<String, En.Edu> collAndEduMap = Guidelines.getCollAndEduMap(basicUnivCollTable, "id", "info`,`edu", notCollSet);
////        Guidelines.getEnDeptAndFuDanOrStemListMap(enBasicDeptTable, "id", "info`,`fudan`,`stem", stemSet, fuDanSet, deptAndFuDanOrStemListMap);
////        Guidelines.getCnDeptAndFuDanOrStemListMap(cnBasicDeptTable, "id", "info`,`fudan`,`stem", stemSet, fuDanSet, deptAndFuDanOrStemListMap);
//        Map<String, En.FuDanStemEdu> fuDanStemEduMap = Guidelines.getFuDanStemEduMap(basicHospUnivDeptTable, "id", "info`,`fudan`,`stem`,`edu");
//        if (tableNum.get(0).getInt("count") > 0) {
//            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + summaryTable).intValue();
//            Collection<Integer> tableList = new ArrayList<>();
//            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
//            tableList.parallelStream().forEach(startId -> {
//                try {
//                    List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + summaryTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1) + limit);
//                    System.out.println("\033[31;1m" + format.format(new Date()) + " 读表现在开始：" + startId + " 表名：" + summaryTable + Utils.printMemoryStr() + "\033[0m");
//                    for (Entity entry : tableData) {
//                        Integer year = entry.getInt("year");
//                        String c1Au = entry.getStr("c1_au");
//                        if (null != year && year > 2018 && null != c1Au && c1Au.contains("sort")) {
//                            List<C1Auth> c1s = JSONObject.parseArray(c1Au, C1Auth.class);
//                            List<En.C1Auth> c1List = new ArrayList<>();
//                            for (C1Auth c1 : c1s) {
//                                En.C1Auth c1Auth = new En.C1Auth();
//                                c1Auth.setFull(c1.getFull());
//                                c1Auth.setInfo(c1.getInfo());
//                                c1List.add(c1Auth);
//                            }
//                            Guidelines.setC1List(c1List, standardMap, acadSet, centerSet, collSet, hospSet, univSet, removeSet, countryMap,  deptAndSumMap, dellSet,
//                                    chinaSet,  provCitySet, citySet, subjectSet, deptAndInfoSetMap, unitAndInfoSetMap, unitAndSumMap,toDeptSet,fuDanStemEduMap);
//                        }
//                    }
//                } catch (SQLException e) {
//                    throw new RuntimeException(e);
//                }
//            });
//        }
//
//        // 将科室信息，包含医院、大学的统一给保存进basicHospUnivDeptTable表，可以在这一张表中处理复旦、STEM和教育部学科。
//        Db.use().execute("truncate table " + basicHospUnivDeptTable);
//        Set<String> set = new HashSet<>();
//        for (Map.Entry<String, Integer> entry : deptAndSumMap.entrySet()) {
//            if (entry.getKey().startsWith("[univ]") || entry.getKey().startsWith("[hosp]")) {
//                String dept = entry.getKey().substring("[univ]".length());
//                if (dept.length() > 1) {
//                    set.add(dept);
//                }
//            }
//        }
//        set.parallelStream().forEach(dept -> {
//            Set<String> univAdnInfoSet = (deptAndInfoSetMap.containsKey("[univ]" + dept)) ? deptAndInfoSetMap.get("[univ]" + dept) : new HashSet<>();
//            Set<String> hospAdnInfoSet = (deptAndInfoSetMap.containsKey("[hosp]" + dept)) ? deptAndInfoSetMap.get("[hosp]" + dept) : new HashSet<>();
//            int univSum = deptAndSumMap.getOrDefault("[univ]" + dept, 0);
//            int hospSum = deptAndSumMap.getOrDefault("[hosp]" + dept, 0);
//            try {
//                Db.use().insert(Entity.create(basicHospUnivDeptTable).set("info", dept).set("univ_c1s", JSONUtil.toJsonStr(univAdnInfoSet)).set("hosp_c1s", JSONUtil.toJsonStr(hospAdnInfoSet))
//                        .set("univ_sum", univSum).set("hosp_sum", hospSum).set("is_univ", univSum > 0 ? 1 : 0).set("is_hosp", hospSum > 0 ? 1 : 0));
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        });
//        Db.use().execute("truncate table " + hospUnivInfoSumTable);
//        unitAndInfoSetMap.keySet().parallelStream().forEach(unit -> {
//            try {
//                Db.use().insert(Entity.create(hospUnivInfoSumTable).set("unit", unit).set("example", JSONUtil.toJsonStr(unitAndInfoSetMap.get(unit))).set("sum", unitAndSumMap.getOrDefault(unit, 0)));
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        });
//
//
//    }
//
//    public static void test(String full, String htLocationChinaTable) throws SQLException {
//        Map<String, String> standardMap = new ConcurrentHashMap<>();
//        standardMap.put("department medical", "Department Medical");
//        Set<String> acadSet = uSet.getAcadSet();
//        Set<String> centerSet = uSet.getCenterSet();
//        Set<String> collSet = uSet.getCollSet();
//        Set<String> hospSet = uSet.getHospSet();
//        Set<String> univSet = uSet.getUnivSet();
//        Set<String> removeSet = uSet.getRemoveSet();
//        Set<String> dellSet = uSet.getDellSet();
//        Set<String> citySet = new HashSet<>();
//        Set<String> subjectSet = cList.getSubjectSet();
//        Set<String> provCitySet = Dept.getProvCitySet(htLocationChinaTable, "id", "prov`,`name", citySet);
//
//        Set<String> unitSet = new HashSet<>();
//        Set<String> deptSet = new HashSet<>();
//        if (Utils.isContainChinese(full)) {
//            boolean isCnHosp = full.contains("医院") || full.contains("疾病预防控制中心") || full.contains("肿瘤防治中心") || full.contains("预防控制中心") || full.contains("医疗救治中心") || full.contains("眼病防治中心") || full.contains("血液病研究所") ||
//                    full.contains("传染病防治院") || full.contains("眼科研究所") || full.contains("心肺研究所") || full.contains("肾病研究所") || full.contains("妇幼保健院") || full.contains("妇婴保健院") || full.contains("妇女保健院") ||
//                    full.contains("儿科研究所") || full.contains("癌症研究所") || full.contains("诊疗中心") || full.contains("医学中心") || full.contains("医疗中心") || full.contains("眼科中心") || full.contains("卫生中心") || full.contains("体检中心") ||
//                    full.contains("临床中心") || full.contains("精神病院") || full.contains("健康中心") || full.contains("急救中心") || full.contains("病研究所") || full.contains("保健中心") || full.contains("癌症中心") || full.contains("防疫站") || full.contains("医 院");
//            boolean isCnUniv = full.contains("大学") || full.contains("学院") || full.contains("学校");
//            if (isCnHosp || isCnUniv) {
//                Unit.getCnUnit(full, unitSet);
//                Dept.getCnDept(full, provCitySet, deptSet, citySet, unitSet);
//                // 如果没有获取到学科的集合（即部门信息，比如仅有山东大学齐鲁医院，后面没有什么科室信息的），而又属于大学和医院的，对大学的学科加上"临床医学"，即认为大学的附属医院为"临床医学"学科。
//                if (deptSet.isEmpty() && isCnUniv && isCnHosp) {
//                    deptSet.add("[univ]" + "临床医学");
//                }
//            }
//        } else {
//            boolean isEnUniv = Unit.isUniv(full, univSet, removeSet);
//            boolean isEnHosp = Unit.isHosp(full, hospSet, removeSet);
//            if (isEnUniv || isEnHosp) {
//                Set<String> depts = new HashSet<>();
//                Dept.getEnDept(full, depts, subjectSet, removeSet);
//                Set<String> units = Unit.getEnUnit(full, standardMap, acadSet, centerSet, collSet, hospSet, univSet, removeSet, dellSet);
//                for (String s : units) {
//                    unitSet.add(s);
//                    depts.remove(s);
//                }
//                for (String s : depts) {
//                    if (isEnUniv) {
//                        deptSet.add("[univ]" + s);
//                    }
//                    if (isEnHosp) {
//                        deptSet.add("[hosp]" + s);
//                    }
//                }
//                if (deptSet.isEmpty() && isEnUniv && isEnHosp) {
//                    deptSet.add("[univ]" + "临床医学");
//                }
//            }
//        }
//        for (String original : deptSet) {
//            System.out.println(original);
//        }
//    }
//
//    public static void getInfoFuDanStemYouDao(String tableName, String autoId, String field, Map<String, InfoFuDanStemEdu> infoFuDanStemYouDaoMap, String type) throws SQLException {
//        int onePageNum = 1000;
//        List<Entity> tableNum = Db.use().query("select count(1) as count from " + tableName);
//        if (tableNum.get(0).getInt("count") > 0) {
//            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + tableName).intValue();
//            Collection<Integer> tableList = new ArrayList<>();
//            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
//            for (Integer startId : tableList) {
//                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + tableName + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
//                for (Entity entry : tableData) {
//                    String info = Utils.getStrField(entry, "info").toLowerCase().trim();
//                    if ("stem".equals(type)) {
//                        String fuDan = Utils.getStrField(entry, "fudan");
//                        String stem = Utils.getStrField(entry, "stem");
//                        String youDao = Utils.getStrField(entry, "you_dao");
//                        if (info.length() > 1 && youDao.length() > 1) {
//                            InfoFuDanStemEdu infoFuDanStemYouDao = new InfoFuDanStemEdu();
//                            infoFuDanStemYouDao.setInfo(info);
//                            infoFuDanStemYouDao.setYouDao(youDao);
//                            if (fuDan.contains("\"}]")) {
//                                infoFuDanStemYouDao.setFuDan(fuDan);
//                            }
//                            if (stem.contains("\"}]")) {
//                                infoFuDanStemYouDao.setStem(stem);
//                            }
//                            infoFuDanStemYouDaoMap.put(info, infoFuDanStemYouDao);
//                        }
//                    } else if ("edu".equals(type)) {
//                        String edu = Utils.getStrField(entry, "edu");
//                        if (info.length() > 1 && edu.contains("category")) {
//                            InfoFuDanStemEdu infoFuDanStemYouDao = new InfoFuDanStemEdu();
//                            infoFuDanStemYouDao.setInfo(info);
//                            infoFuDanStemYouDao.setEdu(edu);
//                            infoFuDanStemYouDaoMap.put(info, infoFuDanStemYouDao);
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//    public static void updateBasicHospUnivDeptTable(String basicHospUnivDeptTable, String autoId, String field, Map<String, InfoFuDanStemEdu> infoFuDanStemYouDaoMap, String type) throws SQLException {
//        int onePageNum = 1000;
//        List<Entity> tableNum = Db.use().query("select count(1) as count from " + basicHospUnivDeptTable);
//        if (tableNum.get(0).getInt("count") > 0) {
//            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + basicHospUnivDeptTable).intValue();
//            Collection<Integer> tableList = new ArrayList<>();
//            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
//            for (Integer startId : tableList) {
//                List<Entity> tableData = Db.use().query("select `" + autoId + "`,`" + field + "` from " + basicHospUnivDeptTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
//                for (Entity entry : tableData) {
//                    String info = Utils.getStrField(entry, "info").toLowerCase().trim();
//                    int id = Utils.getIntField(entry, autoId);
//                    if (infoFuDanStemYouDaoMap.containsKey(info) && id > 0) {
//                        InfoFuDanStemEdu infoFuDanStemYouDao = infoFuDanStemYouDaoMap.get(info);
//                        if ("stem".equals(type)) {
//                            try {
//                                Db.use().update(
//                                        Entity.create().set("fudan", infoFuDanStemYouDao.getFuDan())
//                                                .set("stem", infoFuDanStemYouDao.getStem())
//                                                .set("you_dao", infoFuDanStemYouDao.getYouDao()),
//                                        Entity.create(basicHospUnivDeptTable).set(autoId, id));
//                            } catch (SQLException e) {
//                                e.printStackTrace();
//                            }
//                        } else if ("edu".equals(type)) {
//                            try {
//                                Db.use().update(Entity.create().set("edu", infoFuDanStemYouDao.getEdu()), Entity.create(basicHospUnivDeptTable).set(autoId, id));
//                            } catch (SQLException e) {
//                                e.printStackTrace();
//                            }
//                        }
//                    }
//                }
//            }
//        }
//    }
//
//
//    @Data
//    public static class C1Auth implements Serializable {
//        private static final long serialVersionUID = 1L;
//        private List<String> authList = new ArrayList<>(); // 原始的作者的集合
//        private Set<String> unitSet = new HashSet<>(); // 单位的集合，含医院、大学及其它
//        private Set<String> stdUnit = new HashSet<>(); // 单位的标准名称的集合，即规范后的数据
//        private String country = ""; // 国家的中文名称
//        //        private Set<En.Edu> eduSet = new HashSet<>(); // 教育部学科
//        private String info = ""; // 原始的单位信息
//        private String full = ""; // 原始的单位信息的全写
//        private Set<String> deptSet = new HashSet<>(); // 原始的科室信息
//        private Set<String> stemSet = new HashSet<>(); // STEM的科室名称
//        private Set<String> fuDanSet = new HashSet<>(); // 复旦大学排行榜中的科室名称
//        private int sort = 0; // 作者的排序，第一作者为1，不是计算机中的0。
//    }
//
//    @Data
//    public static class InfoFuDanStemEdu implements Serializable {
//        private static final long serialVersionUID = 1L;
//        private String info = "";
//        private String fuDan = "";
//        private String stem = "";
//        private String youDao = "";
//        private String edu = "";
//    }
//
//
//}
